package cn.net.cfss.fgbp.provider.uid.impl;

import cn.net.cfss.fgbp.api.uid.inte.IUidService;
import cn.net.cfss.fgbp.provider.uid.exception.ErrorCountException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;

/**
 * 全局ID获取接口
 *
 * @author yudeng
 * @since 2019-04-29
 */
@RestController
@Slf4j
public class UidServiceImpl implements IUidService {

    @Value("${uid.count.limit:1000}")
    private int uidCountLimit;

    @Autowired
    private CachedUidGenerator cachedUidGenerator;

    /**
     * 获取整型ID
     *
     * @return
     */
    @Override
    public Long getUid() {
        Long uid = cachedUidGenerator.getUID();
        return uid;
    }

    /**
     * 批量获取整型ID
     *
     * @param count 需要的ID个数
     * @return
     */
    @Override
    public List<Long> getBatchUid(Integer count) {
        if (count.intValue() <= 0 || count.intValue() > uidCountLimit) {
            throw new ErrorCountException("count必须介于0和".concat(String.valueOf(uidCountLimit)).concat("之间"));
        }
        List<Long> uidList = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            uidList.add(cachedUidGenerator.getUID());
        }
        return uidList;
    }

}
